package backend.project.gym_management.service.impl;

import backend.project.gym_management.pojo.dto.UserHealData;
import backend.project.gym_management.pojo.vo.*;
import backend.project.gym_management.mapper.UserOperationMapper;
import backend.project.gym_management.pojo.entity.PayOrder;
import backend.project.gym_management.pojo.dto.UserSelectCourseParam;
import backend.project.gym_management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserOperationMapper userOperationMapper;

    @Override
    public List<UserCourseListVO> listCourses(Long uid) {
        log.debug("开始处理查询课程列表请求，用户ID：{}", uid);
        List<UserCourseListVO> courses = userOperationMapper.listCoursesByUid(uid);
        log.debug("查询课程列表成功，结果：{}", courses);
        return courses;
    }

    @Override
    public UserCourseDetailVO getCourseDetail(Long cid) {
        log.debug("开始处理查询课程详情请求，参数：{}", cid);
        UserCourseDetailVO detail = userOperationMapper.getCourseDetail(cid);
        log.debug("查询课程详情成功，结果：{}", detail);
        return detail;
    }

    @Override
    public UserEnrollmentInfoVO selectCourse(UserSelectCourseParam param) {
        int rows = userOperationMapper.selectCourse(param);
        if (rows <= 0) {
            throw new RuntimeException("选课失败");
        }

       UserEnrollmentInfoVO result = userOperationMapper.getSelectedCourseInfo(param.getCid(), param.getUid());

        if (result == null) {
            throw new RuntimeException("选课信息查询失败");
        }

        return result;
    }
    @Override
    public UserHealthDataVO getHealthDataByUid(Long uid) {
        log.debug("开始处理查询用户健康数据请求，用户ID：{}", uid);
        UserHealthDataVO healthData = userOperationMapper.getHealthDataByUid(uid);
        log.debug("查询用户健康数据成功，结果：{}", healthData);
        return healthData;
    }

    @Override
    public UserDetailsVO getUserDetails(Long uid) {
        log.debug("开始处理查询用户详细信息请求，用户ID：{}", uid);
        UserDetailsVO userDetails = userOperationMapper.getUserDetails(uid);
        log.debug("查询用户详细信息成功，结果：{}", userDetails);
        return userDetails;
    }

    @Override
    public void updateHealthData(UserHealData healthData) {
        log.debug("开始处理插入用户健康数据请求，参数：{}", healthData);
        if(userOperationMapper.getHealthDataByUid(healthData.getUid()) == null){
            userOperationMapper.insertHealthData(healthData);
        }else {
            userOperationMapper.updateHealthData(healthData);
        }
        log.debug("插入用户健康数据成功");
    }

    @Override
    public Long insertOrder(PayOrder order) {
        log.debug("开始处理插入用户订单信息请求，参数：{}", order);
        int rows = userOperationMapper.insertOrder(order);
        Long oid = 0L;
        if (rows > 0) {
            oid = userOperationMapper.selectOid(order.getUid(), order.getCid());
        }
        log.debug("插入用户订单信息成功");
        return oid;
    }

    @Override
    public List<PayOrder> selectOrder(Long uid) {
        log.debug("开始处理查询用户订单信息请求，用户ID：{}", uid);
        List<PayOrder> orders = userOperationMapper.selectOrder(uid);
        log.debug("查询用户订单信息成功，结果：{}", orders);
        return orders;
    }

}
